package com.lab.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lab.pojo.Texperiment;
import com.lab.service.TexService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tex")
public class TexController {
    @Resource
    TexService texService;
    private Map<String,Object> result= new HashMap<>();
    /**
     * 查询所有教学实验信息
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/listByPage")
    public Map<String,Object> listByPage(Integer page, Integer rows){
        //设置分页参数
        page=(page-1)*rows;
        PageHelper.offsetPage(page, rows);
        //查询所有数据
        List<Texperiment> list= texService.allTexperiments();
        //使用PageInfo封装查询结果
        PageInfo<Texperiment> pageInfo=new PageInfo<Texperiment>(list);
        for(Texperiment texperiment:list){
            if(texperiment.getLaState().equals("0")){
                texperiment.setLaState("未通过");
            }else if (texperiment.getLaState().equals("1")){
                texperiment.setLaState("通过");
            }else{
                texperiment.setLaState("待审核");
            }
        }
        //从PageInfo对象取出查询结果
        //总记录数
        long total=pageInfo.getTotal();
        //当前页数据列表
        List<Texperiment> texperimentList=pageInfo.getList();
        result.put("total", total);
        result.put("rows", texperimentList);
        return result;
    }

    /**
     * 添加和修改
     * @param texperiment
     * @return
     */
    @PostMapping("/saveTex")
    public Map<String,Object> saveTexperiment(Texperiment texperiment){
        try {
            texService.saveTexperiment(texperiment);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 数据回显
     * @param teId
     * @return
     */
    @RequestMapping(value="/findTexById",method = RequestMethod.GET)
    public Texperiment findTexById(Long teId) {
        Texperiment t= texService.findTexById(teId);
        return t;
    }
    /**
     *删除数据
     * @param teId
     * @return
     */
    @RequestMapping(value="/deleteTex",method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> deleteTexperiment(Long[] teId){
        try {
            texService.deleteTexperiment(teId);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 教学实验
     * @param texperiment
     * @return
     */
    @PostMapping("/applyTex")
    public Map<String,Object> applyTexperiment(Texperiment texperiment){
        try {
            texperiment.setLaState("2");
            texService.applyTexperiment(texperiment);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }
    /**
     * 审核通过
     * @param teId
     * @return
     */
    @RequestMapping(value="/passTexperiment")
    public Map<String,Object> passTexperiment(Long[] teId){
        try {
            texService.passTexperiment(teId);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 审核不通过
     * @param teId
     * @return
     */
    @RequestMapping(value="/unpassTexperiment")
    public Map<String,Object> unpassOexperiment(Long[] teId){
        try {
            texService.unpassTexperiment(teId);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }
}
